package likou.offer2;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/11/01 13:36
 */
public class _014 {
    public static void main(String[] args) {
        _014 o = new _014();
        System.out.println(o.checkInclusion("hello", "ooolleoooleh"));
    }
    public boolean checkInclusion(String s1, String s2) {
        int len = s1.length();
        int[] cs = new int[26];
        int[] nowcs = new int[26];
        for(int i=0;i<len;i++){
            cs[s1.charAt(i)-'a']++;
        }
        int now = 0;
        char[] s2cs = s2.toCharArray();
        int left =0;
        while(left<s2cs.length){
            int right = left;
            for(;right<s2.length();right++){
                char c = s2cs[right];
                if(nowcs[c-'a']+1<=cs[c-'a']){
                    nowcs[c-'a']++;
                    now++;
                    if(now==len) return true;
                }else{
                    if(cs[c-'a']>0){
                        while(c!=s2cs[left]){
                            nowcs[s2cs[left++]-'a']--;
                            now--;
                        }
                        left++;
                    }else{
                        now = 0;
                        nowcs = new int[26];
                        left=right+1;
                        break;
                    }
                }
            }
            if (right==s2cs.length) break;
        }
        return false;
    }
}
